package com.patni.frame.util.edge;

import java.awt.Cursor;
import java.awt.MouseInfo;
import com.patni.frame.util.Common;
public class AidEdge extends Thread{
private int x;
private int y;
private int width;
private int height;

private final int space = 3;

private int cursorType = -1;
private int tempX = 0;
private int tempY = 0;

private boolean flag = true;

private Integer[] rectangle = new Integer[4];

public void setModel(int tempX, int tempY, int cursorType){
	this.tempX = tempX;
	this.tempY = tempY;
	this.cursorType = cursorType;
}

public void drawRect(int x, int y, int width, int height){

	this.x = x;
	this.y = y;
	this.width = width;
	this.height = height;
	
	rectangle[0] = x - space;
	rectangle[1] = y - space;
	rectangle[2] = width + 2*space;
	rectangle[3] = height + 2*space;
	
	StaticChips.prepareForPaint(rectangle)         ;                 
} 


@Override
public void run() {
	while(true){
	drawSelection();
	try {
		Thread.sleep(20);
	} catch (InterruptedException e) {
		e.printStackTrace();
	}
	}
}

public void  drawSelection() {
	if(flag){
	int X = MouseInfo.getPointerInfo().getLocation().x;
	int Y = MouseInfo.getPointerInfo().getLocation().y;

	switch(cursorType){
	case Cursor.N_RESIZE_CURSOR : 
		if(Y < y + height - Common.panel_min_height){
			drawRect(x, Y, width, y + height - Y);
		}
		break;
	case Cursor.E_RESIZE_CURSOR :
		if(X > x + width - Common.panel_min_width){
			drawRect(x, y, X - x,  height);
		}
		break;
	case Cursor.S_RESIZE_CURSOR :
		if(Y > y + height - Common.panel_min_height){
			drawRect(x, y, width, Y - y );
			}
		break;
	case Cursor.W_RESIZE_CURSOR :
		if(X < x + width - Common.panel_min_width){
			drawRect(X, y, x + width - X, height);
		}
		break;
	case Cursor.NW_RESIZE_CURSOR : 
		if(Y < y + height - Common.panel_min_height && X < x + width - Common.panel_min_width){
			drawRect(X, Y, x + width - X, y + height - Y);
		}else if(Y < y + height - Common.panel_min_height && X > x + width - Common.panel_min_width){
			drawRect(x + width - Common.panel_min_width, Y, Common.panel_min_width, y + height - Y);
		}else if(Y > y + height - Common.panel_min_height && X < x + width - Common.panel_min_width){
			drawRect(X, y + height - Common.panel_min_height, x + width - X, Common.panel_min_height);
		}
		break;
	case Cursor.NE_RESIZE_CURSOR :
		if(Y < y + height - Common.panel_min_height && X > x + Common.panel_min_width){
			drawRect(x, Y, X - x, y + height - Y);
		}else if(Y > y + height - Common.panel_min_height && X > x + Common.panel_min_width){
			drawRect(x, y + height - Common.panel_min_height, X - x, Common.panel_min_height);
		}else if(Y < y + height - Common.panel_min_height && X < x + Common.panel_min_width){
			drawRect(x, Y, Common.panel_min_width, y + height - Y);
		}
		break;
	case Cursor.SW_RESIZE_CURSOR :
		if(Y > y + Common.panel_min_height && X < x + width - Common.panel_min_width){
			drawRect(X, y, x + width - X, Y - y);
		}else if(Y > y + Common.panel_min_height && X > x + width - Common.panel_min_width){
			drawRect(x + width - Common.panel_min_width, y, Common.panel_min_width, Y - y);
		}else if(Y < y + Common.panel_min_height && X < x + width - Common.panel_min_width){
			drawRect(X, y, x + width - X, Common.panel_min_height);
		}
		break;
	case Cursor.SE_RESIZE_CURSOR :
		if(Y > y + Common.panel_min_height && X > x + Common.panel_min_width){
			drawRect(x, y, X - x, Y - y);
		}else if(Y < y + Common.panel_min_height && X > x + Common.panel_min_width){
			drawRect(x, y, X - x, Common.panel_min_height);
		}else if(Y > y + Common.panel_min_height && X < x + Common.panel_min_width){
			drawRect(x, y, Common.panel_min_width, Y - y);
		}
		break;
	default : 
		drawRect(X, Y, width,height);
		break;
	}
			
	}
}

public void show(int x, int y, int width, int height){
	this.x = x;
	this.y = y;
	this.width = width;
	this.height = height;
}

public int getX() {
	return x;
}

public void setX(int x) {
	this.x = x;
}

public int getY() {
	return y;
}

public void setY(int y) {
	this.y = y;
}

public int getWidth() {
	return width;
}

public void setWidth(int width) {
	this.width = width;
}

public int getHeight() {
	return height;
}

public void setHeight(int height) {
	this.height = height;
}

public boolean isFlag() {
	return flag;
}

public void setFlag(boolean flag) {
	this.flag = flag;
}

public static void main(String[] args) {
	StaticChips.init();
	
	AidEdge ae = new AidEdge();
	ae.setModel(100, 100, Cursor.NE_RESIZE_CURSOR);
	ae.show(50, 50, 300, 460);
	ae.start();
}
}
